package Distributive;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

/**
 * 
 * @author CAT
 * 
 */

public class gameRunner implements Runnable {

	private IBuffer sharedBuffer;
	private Socket producerSocket;
	private casinoRoyale theGame;

	private ObjectOutputStream output;

	/**
	 * 
	 * @param clientSocket
	 * @param shared
	 * @param casino
	 * @throws IOException
	 */
	public gameRunner(Socket clientSocket, IBuffer shared, casinoRoyale casino)
			throws IOException {
		System.out.println("in gameRunner Constructor");
		this.producerSocket = clientSocket;
		this.sharedBuffer = shared;
		this.theGame = casino;

		try {
			this.output = new ObjectOutputStream(this.producerSocket
					.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * trys storeUserInputInBuffer sets the sharedBuffer value to "bye" Exits
	 */
	public void run() {
		try {
			storeUserInputInBuffer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.sharedBuffer.setValue("bye");
		System.exit(0);
	}

	/**
	 * @throws IOException
	 *             if theGame.sendStuff throws an IO exception sets the value of
	 *             the sharedBuffer to theGame.sendCards()
	 */
	private void storeUserInputInBuffer() throws IOException {
		String userInput = "";

		while (!userInput.endsWith("bye")) {
			userInput = theGame.sendCards();
			this.sharedBuffer.setValue(userInput);
		}
	}
}